package com.af.security.auth.util.handler;

import com.af.service.AfI18nService;
import com.af.service.AfRedirectService;
import com.af.system.util.AfServletUtils;
import com.szy.system.api.result.ResultUtils;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Locale;

/**
 * @author : zhenyun.su
 * @comment :
 * @since : 2020/5/8
 */
@Component
public class AfNotLoginHandler implements AuthenticationEntryPoint {
    private AfRedirectService redirectService;
    private AfI18nService i18nService;
    public AfNotLoginHandler(AfRedirectService redirectService, AfI18nService i18nService) {
        this.redirectService = redirectService;
        this.i18nService = i18nService;
    }

    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        Locale locale = AfServletUtils.getRequestLocale(httpServletRequest);
        String msg = i18nService.getI18n("alert.security.not-login", null, locale);
        redirectService.redirectJson(httpServletResponse, ResultUtils.failure(msg, e.getMessage()));
    }
}
